<?php
/**
 *需求模型
 *@author shipanpan
 */
class Backup_Model_Story extends Application_Model_Story {
	public function __construct() {
		$this->db = $this->db();
		$this->writeDb = $this->db("write_db");
	}

	/**
	 * 获取需求需要备货的产品数量
	 * @param  string $goodsSn       sku
	 * @param  string $warehouseCode 商品所在仓库
	 * @param string  $platformWarehouse 商品所属的平台仓
	 * @return [type]                [description]
	 */
	public function getBackupQuantity(string $goodsSn, string $warehouseCode, $platformWarehouse = "", $date) {
		$story = $this->db->getOne($goodsSn, $warehouseCode, $platformWarehouse, $date);
		if ($story) {
			return array_sum($story['a_story_quantity'] + $story['b_story_quantity'] + $story['c_story_quantity']);
		}
		return 0;
	}

	/**
	 * 获取备货需求的调拨需求详情
	 * @param  [type] $stroyId [description]
	 * @return [type]          [description]
	 */
	public function getStoryTransferApply(int $stroyId) {
		$storyTransferApplyId = $this->getStoryTransferApplyId($stroyId);
		if ($storyTransferApplyId) {
			return false;
		}

		return $this->db->fetchRow("select *  from b_stock_transfer_log stock_transfer_apply_id = ?", $storyTransferApplyId);
	}

	/**
	 * 获取备货需求申请调拨的记录的id
	 * @param  [type] $stroyId [description]
	 * @return [type]          [description]
	 */
	public function getStoryTransferApplyId(int $stroyId) {
		return $this->db->query("select transfer_apply_id from b_stock_transfer_log story_id = ?", $stroyId)->fetchOne();
	}

	/**
	 * 需求审核
	 * @param  int    $storyId       需求id
	 * @param  int    $operationType 审核操作类型
	 * @return [type]                [description]
	 */
	public function check(int $storyId, int $operationType) {
		$storyInfo = $this->getOneById($storyId);
		if (!$storyInfo) {
			return $this->_error('未找到需求id为:' . $storyId . '的需求！');
		}
		//操作对应的需求状态
		$operationTypeNeedStatus = [
			self::PRIMARY_TEAM_LEADER_AGREE_OPERATION => self::WAIT_PRIMARY_TEAM_LEADER_EXAMINE, //小组长审核通过
			self::ADVANCED_TEAM_LEADER_AGREE_OPERATION => self::WAIT_ADVANCED_TEAM_LEADER_EXAMINE, //大组长审核通过
			self::TEAM_MANAGER_AGREE_OPERATION => self::WAIT_TEAM_MANAGER_EXAMINE, //主管审核通过
			self::MANAGER_AGREE_OPERATION => self::WAIT_MANAGER_EXAMINE, //经理审核通过
			self::PRIMARY_TEAM_LEADER_REJECT_OPERATION => self::WAIT_PRIMARY_TEAM_LEADER_EXAMINE, //小组长驳回
			self::ADVANCED_TEAM_LEADER_REJECT_OPERATION => self::WAIT_ADVANCED_TEAM_LEADER_EXAMINE, //大组长驳回
			self::TEAM_MANAGER_REJECT_OPERATION => self::WAIT_TEAM_MANAGER_EXAMINE, //主管驳回
			self::MANAGER_REJECT_OPERATION => self::WAIT_MANAGER_EXAMINE, //经理理驳回
		];
		if (!in_array($operationType, array_keys($operationTypeNeedStatus))) {
			return $this->_error('非法审核操作类型：' . $operationType);
		}

		$storyCurStatus = $storyInfo['examine_status'];
		$operationNeedStatus = $operationTypeNeedStatus[$operationType];
		if ($storyCurStatus != $operationNeedStatus) {
			return $this->_error("需求当前为'" . self::ALL_EXAMINE_STATUS[$storyCurStatus] . "'状态,不能进行" . self::getCheckOperationName($operationType) . "操作！");
		}

		$waitCheckStatusClassMap = [
			self::WAIT_PRIMARY_TEAM_LEADER_EXAMINE => 'Application_Model_StoryStatus_PrimaryTeamLeaderStatus',
			self::WAIT_ADVANCED_TEAM_LEADER_EXAMINE => 'Application_Model_StoryStatus_AdvancedTeamLeaderStatus',
			self::WAIT_TEAM_MANAGER_EXAMINE => 'Application_Model_StoryStatus_TeamManagerStatus',
			self::WAIT_MANAGER_EXAMINE => 'Application_Model_StoryStatus_ManagerStatus',
		];

		$statusClass = $waitCheckStatusClassMap[$operationNeedStatus];
		//是否是审核通过
		$isPassOperation = true;
		$rejectOperationType = [
			self::PRIMARY_TEAM_LEADER_REJECT_OPERATION,
			self::ADVANCED_TEAM_LEADER_REJECT_OPERATION,
			self::TEAM_MANAGER_REJECT_OPERATION,
			self::MANAGER_REJECT_OPERATION,
		];
		if (in_array($operationType, $rejectOperationType)) {
			$isPassOperation = false;
		}
		$storyStatusObj = new $statusClass($storyId);

		try {
			$storyStatusObj->check($isPassOperation);
			return true;
		} catch (Exception $e) {
			return $this->_error($e->getMessage());
		}
	}

	public function getExamineLog(int $storyId) {
		return $this->db->fetchAll('select * from b_story_examine_log where story_id=' . $storyId);
	}
}